package com.geekaca.jdbc;

import com.geekaca.jdbc.pojo.Account;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TestORM {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://127.0.0.1:3306/dbemp?useSSL=false&characterEncoding=utf8";
        String username = "root";
        String password = "123456";

        Connection connection = DriverManager.getConnection(url, username, password);
        String sql = "select * from account";

        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
//        System.out.println(resultSet);

        List<Account> accountList = new ArrayList<>();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            double money = resultSet.getInt("money");

//            System.out.println(id + " " + name + " " + money);
            // 每一行记录保存在一个对象中
            Account acc = new Account();
            acc.setId(id);
            acc.setName(name);
            acc.setMoney(money);
            accountList.add(acc);

        }
        for (int i = 0; i < accountList.size(); i++) {
            System.out.println(accountList.get(i));
        }
        resultSet.close();
        statement.close();
        connection.close();

    }
}
